package com.qwc.blog.web.blog;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwc.blog.common.annotation.BlogController;
import com.qwc.blog.common.constant.CommonConstant;
import com.qwc.blog.common.constant.ConfigConstant;
import com.qwc.blog.entity.*;
import com.qwc.blog.entity.dto.BlogArticleDto;
import com.qwc.blog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Arrays;

/**
 * @author qiuwencong
 * @Date 2022/3/8 16:13
 * @Package com.qwc.blog.web.blog
 * @Description
 */
@BlogController
public class HomeController {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private IClassifyService classifyService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ITagService tagService;

    @Autowired
    private IBlogLogService blogLogService;

    @Autowired
    private IContentService contentService;

    @Autowired
    private IDynamicService dynamicService;

    @GetMapping("desc")
    public ResponseEntity<Object> getAuthorDesc() {
        return ResponseEntity.ok(sysConfigService.list(Wrappers.<SysConfig>lambdaQuery()
                .eq(SysConfig::getName, ConfigConstant.INFORMATION)
                .eq(SysConfig::getStatus, CommonConstant.ABLE)));
    }

    @GetMapping("tags")
    public ResponseEntity<Object> getTags(){
        return ResponseEntity.ok(tagService.listFromCache());
    }

    @GetMapping("/random/articles")
    public ResponseEntity<Object> getRandomArticles(){
        return ResponseEntity.ok(articleService.listFromCache());
    }

    @GetMapping("/articles/page")
    public ResponseEntity<Object> getPageArticles(@Validated BlogArticleDto dto){
        return ResponseEntity.ok(articleService.pageWithTag(dto));
    }

    @GetMapping("/classify/{pid}")
    public ResponseEntity<Object> getClassifyByPId(@PathVariable Integer pid){
        return ResponseEntity.ok(classifyService.getChildren(pid));
    }

    @GetMapping("/classify/tree/{pid}")
    public ResponseEntity<Object> getClassifyTreeByPid(@PathVariable Integer pid){
        return ResponseEntity.ok(classifyService.getTreeByPid(pid));
    }

    @GetMapping("/classify/parent/{classifyId}")
    public ResponseEntity<Object> getClassifyParent(@PathVariable Integer classifyId){
        return ResponseEntity.ok(classifyService.getClassifyParent(classifyId));
    }

    @GetMapping("/log/{current}")
    public ResponseEntity<Object> getBlogLogs(@PathVariable Integer current){
        Page<BlogLog> blogLogPage = new Page<>();
        blogLogPage.setCurrent(current);
        return ResponseEntity.ok(blogLogService.page(blogLogPage,Wrappers.<BlogLog>lambdaQuery().orderByDesc(BlogLog::getCreateTime)));
    }

    @GetMapping("/content/{articleId}")
    public ResponseEntity<Object> getContent(@PathVariable Integer articleId){
        return ResponseEntity.ok(contentService.getOne(Wrappers.<Content>lambdaQuery().eq(Content::getArticleId,articleId)));
    }

    @GetMapping("/dynamic/{current}")
    public ResponseEntity<Object> getDynamic(@PathVariable Integer current){
        Page<Dynamic> dynamicPage = new Page<>();
        dynamicPage.setCurrent(current);
        return ResponseEntity.ok(dynamicService.page(dynamicPage,Wrappers.<Dynamic>lambdaQuery()
                .orderByDesc(Arrays.asList(Dynamic::getTopTime,Dynamic::getCreateTime))));
    }
}
